import time

import localizing
import dialog
import startmenu
import logger
import save
import player
from mainloop import Mainloop;
import game_statistics
import save_data

import enemies

from ui import print, input, endPassage

# 负责游戏主要逻辑

class Game:
    """
    游戏对象，管理游戏运行，状态。
    """
    I = None
    def __init__(self) -> None:
        if not Game.I: Game.I = self

        # localizer 需要使用logger, 所以先初始化 logger 。
        self.logger = logger.Logger(True)
        self.localizer = localizing.Localizer()
        self.save = None
        
        self.status = {}
        
        # 注册
        self.register()
        
    def register(self) -> None:
        self.enemyTypesManager = enemies.EnemyTypesManager()
        enemies.registerAll(self.enemyTypesManager)


    def start(self) -> None:
        # 初始化
        status = self.status = {}
        status["where"] = "startmenu"
        status["player"] = None
        status["save"] = self.save = save.Save()
        # 读取全局存档
        newGlobalSave = self.save.readGlobalFromLocal()
        # 统计数据
        status["statistics"] = self.statistics = game_statistics.Statistics()
        status["saveData"] = self.saveData = save_data.SaveData()
        
        # 注册全局存档相关对象
        self.save.registerSave([
            self.statistics,
            self.localizer
        ])

        # 懒得解释
        if newGlobalSave:
            self.localizer.loadLanguage(self.localizer.currentLanguage)
            self.save.doSaveGlobalFuncs()
        else:
            self.save.doReadGlobalFuncs()
        
        cm = dialog.ChoiceManager()
        
        # 开始菜单
        while True:    
            startmenu.StartMenu.choicesStartMenu(cm)
            if (status["where"] == "game") : break
            elif (status["where"] == "monuments"): startmenu.Monuments.choicesMonuments(cm)
            elif (status["where"] == "settings"): startmenu.Settings.choicesSettingsRoot(cm)
            elif (status["where"] == "exit"): return

        self.save.doSaveGlobalFuncs()
        # 开始游戏，初始化
        # 新的玩家对象
        status["player"] = currentPlayer = player.Player()

        # 读取存档
        newSave = self.save.readFromLocal()
        
        # 注册存档相关对象
        self.save.registerSave([
            currentPlayer,
            self.saveData
        ])
        
        # 判断是新存档还是旧的
        if newSave:
            self.save.doSaveFuncs()
        else:
            self.save.doReadFuncs()

        # 游戏主循环
        Mainloop.mainloop(cm)

        # 局数统计
        if status["where"] == "gameover":
            self.saveData.endTime = time.time()
            self.statistics.gameTime += ( self.saveData.endTime - self.saveData.startTime )
            self.statistics.gameRound += 1
        # 保存
        self.save.doSaveFuncs()
        self.save.doSaveGlobalFuncs()
        self.save.save()
        
        if status["where"] == "gameover":
            # 吃灰去吧
            while True:
                name = input( self.localizer.get("textdungeon.gameover.monument.name") )
                self.save.data["name"] = name
                self.save.data["description"] = "描述"
                success = self.save.moveSaveToMonument(name)
                if success: break
                else:
                    print( self.localizer.get("textdungeon.gameover.monument.invalidname") )
                    endPassage()
        
    def saveNow(self) -> None:
        self.save.doSaveFuncs()
        self.save.doSaveGlobalFuncs()
        self.save.save()